package com.ly.mpayment.core.util;



import java.lang.reflect.Type;
import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.ly.mpayment.persist.common.CustomUUID;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
/**
 *
 */

public class GSONFactory {
	
	
	
	public static Gson getGson() {
		JsonSerializer<Date> serializer = new JsonSerializer<Date>() {
			public JsonElement serialize(Date src, Type typeOfSrc,
					JsonSerializationContext context) {
				return src == null ? null : new JsonPrimitive(new Long(src.getTime()));
			}
		};

		JsonDeserializer<Date> deserializer = new JsonDeserializer<Date>() {
			public Date deserialize(JsonElement json, Type type,
					JsonDeserializationContext ctx) throws JsonParseException {
				if(json.getAsString().indexOf("-") > 0){
					return DateUtils.parse(json.getAsJsonPrimitive().getAsString(), DateUtils.YYYY_MM_DD);
				}else {
					return new Date(json.getAsJsonPrimitive().getAsLong());
				}
			}
		};
		
		JsonSerializer<CustomUUID> customUUIDSerializer = new JsonSerializer<CustomUUID>() {

			public JsonElement serialize(CustomUUID src, Type type, JsonSerializationContext arg2) {
				return src == null ? null:new JsonPrimitive(src.getId());
			}
			
		};
		
		JsonDeserializer<CustomUUID> deserializerCustomUUID = new JsonDeserializer<CustomUUID>() {
			public CustomUUID deserialize(JsonElement json, Type type, JsonDeserializationContext arg2) throws JsonParseException {
				return new CustomUUID(json.getAsJsonPrimitive().getAsString());
			}
		};

		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, deserializer)
		.registerTypeAdapter(Date.class, serializer)
		.registerTypeAdapter(CustomUUID.class, customUUIDSerializer)
		.registerTypeAdapter(CustomUUID.class, deserializerCustomUUID);
		Gson gson = builder.create();
		return gson;
	}

	public static Gson getDataStrGson() {
		JsonSerializer<Date> serializer = new JsonSerializer<Date>() {
			public JsonElement serialize(Date src, Type typeOfSrc,
					JsonSerializationContext context) {
				return src == null ? null : new JsonPrimitive(src.getTime()+"");
			}
		};

		JsonDeserializer<Date> deserializer = new JsonDeserializer<Date>() {
			public Date deserialize(JsonElement json, Type type,
					JsonDeserializationContext ctx) throws JsonParseException {
				if(json.getAsString().indexOf("-") > 0){
					return DateUtils.parse(json.getAsJsonPrimitive().getAsString(), DateUtils.YYYY_MM_DD);
				}else {
					return new Date(json.getAsJsonPrimitive().getAsLong());
				}
			}
		};
		
		JsonSerializer<CustomUUID> customUUIDSerializer = new JsonSerializer<CustomUUID>() {

			public JsonElement serialize(CustomUUID src, Type type, JsonSerializationContext arg2) {
				return src == null ? null:new JsonPrimitive(src.getId());
			}
			
		};
		
		JsonDeserializer<CustomUUID> deserializerCustomUUID = new JsonDeserializer<CustomUUID>() {
			public CustomUUID deserialize(JsonElement json, Type type, JsonDeserializationContext arg2) throws JsonParseException {
				return new CustomUUID(json.getAsJsonPrimitive().getAsString());
			}
		};

		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, deserializer)
		.registerTypeAdapter(Date.class, serializer)
		.registerTypeAdapter(CustomUUID.class, customUUIDSerializer)
		.registerTypeAdapter(CustomUUID.class, deserializerCustomUUID);
		Gson gson = builder.create();
		return gson;
	}
	
	public static Gson getDataFormatGson() {
		JsonSerializer<Date> serializer = new JsonSerializer<Date>() {
			public JsonElement serialize(Date src, Type typeOfSrc,
					JsonSerializationContext context) {
				return src == null ? null : new JsonPrimitive(DateUtils.convert(src, DateUtils.YYYY_MM_DD));
			}
		};

		JsonDeserializer<Date> deserializer = new JsonDeserializer<Date>() {
			public Date deserialize(JsonElement json, Type type,
					JsonDeserializationContext ctx) throws JsonParseException {
					return DateUtils.parse(json.getAsJsonPrimitive().getAsString(), DateUtils.YYYY_MM_DD);
			}
		};
		
		JsonSerializer<CustomUUID> customUUIDSerializer = new JsonSerializer<CustomUUID>() {

			public JsonElement serialize(CustomUUID src, Type type, JsonSerializationContext arg2) {
				return src == null ? null:new JsonPrimitive(src.getId());
			}
			
		};
		
		JsonDeserializer<CustomUUID> deserializerCustomUUID = new JsonDeserializer<CustomUUID>() {
			public CustomUUID deserialize(JsonElement json, Type type, JsonDeserializationContext arg2) throws JsonParseException {
				return new CustomUUID(json.getAsJsonPrimitive().getAsString());
			}
		};

		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, deserializer)
		.registerTypeAdapter(Date.class, serializer)
		.registerTypeAdapter(CustomUUID.class, customUUIDSerializer)
		.registerTypeAdapter(CustomUUID.class, deserializerCustomUUID);
		Gson gson = builder.create();
		return gson;
	}
	
	/**
	 * 
	 * @Title toJsonWithoutProperties 
	 * @Description 将Result对象转换为Json字符串
	 * @param @param excludesProperties 需要排除的属性
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String toJsonWithoutProperties(Object obj,String[] excludesProperties) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(CustomUUID.class, new CustomJsonValueProcessor());
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonValueProcessor(){
			public Object processArrayValue(Object value, JsonConfig jsonConfig) {
				if(value instanceof Date && value != null)
					return DateUtils.convert((Date)value, DateUtils.YYYY_MM_DD);
				return null;
			}

			public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
				if(value instanceof Date && value != null){
					return DateUtils.convert((Date)value, DateUtils.YYYY_MM_DD);
				}
				return null;
			}}); 
		if (excludesProperties != null)
			jsonConfig.setExcludes(excludesProperties);
		return JSONObject.fromObject(obj, jsonConfig).toString();
	}
}




